<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ page import="java.sql.*" import="java.io.*"%>
    
<!--main content example start-->
<div id="main">
				

<%!
	//declaring public variables
	private String driver;
	private String connection;
	private String user;
	private String password;
%>		
						
<% 
	//to link back to the login.jsp page, uncomment the following:
	//String sessionFlag = ((String) session.getAttribute("login"));
	driver = application.getInitParameter("oracle_driver");
	connection = application.getInitParameter("oracle_db");
	user = application.getInitParameter("oracledb_user");
	password = application.getInitParameter("oracledb_password");
	
	//set packid to an initial value
	String packid= null;
	
	//grab the package id of something off the sidebar
	try
	{
		//packid turns into null if it doesn't work
	packid = (String)request.getParameter("packid");
	}
	catch (Exception e)
	{
		e.printStackTrace();
	}
%>
<!--If someone clicks here from the main package button, all packages display in a table
    (what's coded below). However, if someone clicks the sidebar, or a link in the table, it calls a javascript,
    lookUpPackage(packId), which then calls a .java file which runs a search on the id 
    passed through the header and displays new information, only the clicked o-->
		<%
		//if there was no packid parameter to get when the page loaded:
		if (packid == null)  //(packid.equals("0"))
		{
			//opening div tag and header:
			out.println("<div id=\"package_display\">");
			out.println("<h2>Available Packages</h2>");
		
				//connect to database using web.xml stored parameters
				Class.forName(driver);
				Connection connect = DriverManager.getConnection(connection, user, password);
				java.sql.Statement stmnt = connect.createStatement();
				
				ResultSet resSet = stmnt.executeQuery("SELECT pkgname as \"Name\", " +
						"pkgstartdate || '  through  ' || pkgenddate as \"Time Frame\", " +
						"pkgdesc as \"Description\", pkgbaseprice as \"Price\"" +
						"from ictoosd.packages order by pkgstartdate");
				ResultSetMetaData resSetMetaData = resSet.getMetaData();
				
				if (resSetMetaData != null)//(resSet.next())
				{
					//print the beginning of the table and row
					out.println("<table><tr>");
					
					//grab each of the headers, put them in their own cell
					for (int i=1; i <= resSetMetaData.getColumnCount(); i++)
					{
						String header = resSetMetaData.getColumnName(i);
						if (i == 1)
						{
							out.println("<th class=\"first\">" + header + "</th>");
						}
						else
						{
							out.println("<th>" + header + "</th>");
						}
					}	
					//print the end of the row
					out.println("</tr>");
				}
				//set a counter flag
				int counter = 1;
				String rowType;
				
				while (resSet.next())
				{
					//evaluate if the counter is odd or even
					if (counter%2 == 1)
					{
						//result is odd, so make the css class match
						rowType = "\"row-odd\"";
					}
					else
					{
						rowType = "\"row-even\"";
					}
					
					//print the beginning of the row
					out.println("<tr class=" + rowType + ">");
					
					//grab each of the column data, put them in their own cell
					
					for (int r=1; r <= resSetMetaData.getColumnCount(); r++)
					{
						String cell = "";
						if (resSet.getString(r) != null)
						{
							cell = resSet.getString(r);
						}
						String openingTd = "<td>";
						if (r == 1)
						{
							openingTd = "<td class=\"first\">";
						}
						//if it's the last column (with the price),
						if (r == resSetMetaData.getColumnCount())
						{
								cell = "$" + resSet.getString(r);;
						}
						out.println(openingTd + cell + "</td>");
					}	
					//print the end of the row
					out.println("</tr>");
				}
				//end the table
				out.println("</table>");
				
				//close connection
				connect.close();
				
				//print the last div and a few breaks
				out.println("</div><br/><br/>");
			}
		else  //look up the package referenced and display below:
		{
			try 
		    {			
				//connect to database using web.xml stored parameters
				Class.forName(driver);
				Connection pkgConn = DriverManager.getConnection(connection, user, password);
				java.sql.Statement stmntPackId = pkgConn.createStatement();
									
				ResultSet resSetPackId = stmntPackId.executeQuery("SELECT pkgname as \"Name\", " +
							"pkgstartdate || '  through  ' || pkgenddate as \"Time Frame\", " +
							"pkgdesc as \"Description\", pkgbaseprice as \"Price\" " +
							"from ictoosd.packages where packageid=" + packid);
				
				/*
				resSetPackId.getString(1) is package name
				resSetPackId.getString(2) is time frame
				resSetPackId.getString(3) is description
				resSetPackId.getString(4) is price
				*/
				
				out.println("<div id=\"package_display\">");
				while (resSetPackId.next())
				{
					out.println("<h2>" + resSetPackId.getString(1) + "</h2>");
					out.println("<div class=\"billing\">");
					out.println("<p><em>" + resSetPackId.getString(3) + "</em><br />");
					out.println("<strong>Dates: </strong>" + resSetPackId.getString(2) + "<br />");
					out.println("<div class=\"align-right\"><strong>Price: </strong> $" + resSetPackId.getString(4) + "</div></p>");
					out.println("</div>");
				}
				//the </div> at the end of ALL packages
				out.println("</div>");
				
				//close connection
				pkgConn.close();
			} 
		    catch (SQLException sqle) 
		    {
				// TODO Auto-generated catch block
				sqle.printStackTrace();
			} 
		    catch (ClassNotFoundException cnfe) 
		    {
				// TODO Auto-generated catch block
		    	cnfe.printStackTrace();
			}
		}
			 %>
			 



		</div>
<!-- main ends -->	